using System;
using CodePlex.TfsLibrary;
using CodePlex.TfsLibrary.ObjectModel;
using CodePlex.TfsLibrary.RepositoryWebSvc;

namespace CodePlex.CodePlexClientLibrary
{
    [Command("update", "Merge changes from the repository into the local copy.", AltName = "up", MaxArgs = 1, UsageSummary = "[path] [options]",
        UsageDescription = @"The first column contains update information:
  A  Added
  C  Conflicted
  D  Deleted
  M  Merged automatically
  R  Renamed
  U  Updated")]
    public class UpdateCommand : AuthenticatedCommand
    {
        bool nonRecursive;
        int revision;

        [Option("non-recursive", "do not recurse into subdirectories", ShortName = "n")]
        public bool NonRecursive
        {
            get { return nonRecursive; }
            set { nonRecursive = value; }
        }

        [Option("revision", "get a specific change set", ShortName = "r")]
        public int Revision
        {
            get { return revision; }
            set { revision = value; }
        }

        public override void Execute()
        {
            string localPath = ".";
            VersionSpec version;

            if (Arguments.Count > 0)
                localPath = Arguments[0];

            localPath = CodePlexClientUtil.GetFullPath(localPath);

            if (revision == Constants.NullChangesetId)
                version = VersionSpec.Latest;
            else
                version = VersionSpec.FromChangeset(revision);

            Engine.Update(localPath, !nonRecursive, version, delegate(SourceItem item,
                                                                      UpdateAction actionTaken,
                                                                      SourceItemResult result)
                                                             {
                                                                 OnSourceItem(item, result, actionTaken, localPath);
                                                             });
        }

        static void OnSourceItem(SourceItem item,
                                 SourceItemResult result,
                                 UpdateAction actionTaken,
                                 string localPath)
        {
            if (result != SourceItemResult.S_Ok)
                ConsoleUtil.PrintCommandError(item.LocalName, result);
            else
            {
                string relativePath = CodePlexClientUtil.GetRelativePath(localPath, item.LocalName);

                switch (actionTaken)
                {
                    case UpdateAction.None:
                        Console.WriteLine("Won't clobber existing {0}", relativePath);
                        break;

                    case UpdateAction.Reverted:
                        Console.WriteLine("Reverted {0}", relativePath);
                        break;

                    default:
                        Console.WriteLine("{0}    {1}", actionTaken.ToString().Substring(0, 1), relativePath);
                        break;
                }
            }
        }
    }
}